package com.guava;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import java.util.concurrent.Executors;

/**
 * Created by pengfei on 16/7/27.
 */
public class Test {
    public static void main(String[] args) {
        final EventBus bus = new AsyncEventBus("aa", Executors.newSingleThreadExecutor());
        new Thread(new Runnable(){
            public void run() {
                while(true){
                    bus.post(new MyEvent("a","a"));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                bus.register(new MyListener());
            }
        }).start();
    }
    static class MyListener{
        @Subscribe
        public void consume(String event){
            System.out.println("收到: ");
        }
    }
    static class MyEvent{
        private MyEvent(String type,String msg){
            this.type=type;
            this.msg=msg;
        }
        private String type;
        private String msg;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
